<template>
  <el-card class="box-card" shadow="always">
    <div slot="header" class="clearfix">
      <span>{{ title }}</span>
      <el-button-group style="float: right;">
        <el-button
          v-if="!isShow"
          size="mini"
          icon="el-icon-plus"
          round
          :loading="loadingOptions.loading"
          :disabled="loadingOptions.isDisabled"
          @click="!data.id ? submitForm() : updateData()"
        >{{
          loadingOptions.loadingText }}</el-button>
        <el-button size="mini" icon="el-icon-back" round @click="showCard">返回</el-button>
      </el-button-group>
    </div>
    <div class="body-wrapper">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px" :disabled="isShow">
        <el-form-item label="资源类型" prop="datasourceGroup">
          <el-select v-model="form.type" placeholder="资源类型" style="width: 100%">
            <el-option
              v-for="dict in sourceGroup"
              :key="dict.itemValue"
              :label="dict.itemText"
              :value="dict.itemValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="资源名称" prop="name">
          <el-input v-model="form.name" placeholder="资源名称" />
        </el-form-item>
        <el-form-item label="资源地址" prop="resourcePath">
          <el-input v-model="form.resource_address" placeholder="资源地址" />
        </el-form-item>
        <el-form-item label="服务器地址：" prop="serverIp">
          <span slot="label">服务器地址
            <el-popover placement="right" trigger="hover">
              <p>填写集群主服务的服务器地址（必填）</p>
              <i slot="reference" class="el-icon-info" />
            </el-popover>
          </span>
          <el-input v-model="form.serverIp" placeholder="服务器地址" />
        </el-form-item>
        <el-form-item label="服务器用户名" prop="serverUser">
          <span slot="label">服务器用户名
            <el-popover placement="right" trigger="hover">
              <p>填写集群主服务的服务器用户名（必填）</p>
              <i slot="reference" class="el-icon-info" />
            </el-popover>
          </span>
          <el-input v-model="form.serverUser" placeholder="服务器用户名" />
        </el-form-item>
        <el-form-item label="服务器密码" prop="serverPassword">
          <span slot="label">服务器密码
            <el-popover placement="right" trigger="hover">
              <p>填写集群主服务的服务器密码（必填）</p>
              <i slot="reference" class="el-icon-info" />
            </el-popover>
          </span>
          <el-input v-model="form.serverPassword" placeholder="服务器密码" type="password" />
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
// import * as datasourceApi from '@/api/integration/datax/datax-jdbcDatasource'
import * as user from '@/api/integration/datax/datax-resource'

export default {
  name: 'JdbcDatasourceAdd',
  props: {
    data: {
      type: Object,
      default: function() {
        return {}
      }
    },
    title: { type: String, default: '新增资源' },
    isShow: { type: Boolean, default: false }
  },
  data() {
    return {
      // 展示切换
      showOptions: {
        data: {},
        showList: true,
        showAdd: false,
        showEdit: false,
        showDetail: false
      },
      // 表单校验
      rules: {
        type: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        name: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        serverIp: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        serverUser: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        serverPassword: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      // 保存按钮
      loadingOptions: {
        loading: false,
        loadingText: '保存',
        isDisabled: false
      },
      // 表单参数
      form: {},
      sourceGroup: []
    }
  },
  created() {
    this.getDicts('data_base_type').then(response => {
      if (response.success) {
        this.sourceGroup = response.data
      }
    })
    if (this.data.id) {
      this.getResourceData(this.data.id)
    }
  },
  methods: {
    showCard() {
      this.$emit('showCard', this.showOptions)
    },
    getResourceData(id) {
      user.getDetailResource(id).then(response => {
        if (response.success) {
          this.form = response.data
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loadingOptions.loading = true
          this.loadingOptions.loadingText = '保存中...'
          this.loadingOptions.isDisabled = true
          user.resourceAdd(this.form).then(response => {
            if (response.success) {
              this.$message.success('保存成功')
              setTimeout(() => {
                // 2秒后跳转列表页
                this.$emit('showCard', this.showOptions)
              }, 2000)
            } else {
              this.$message.error('保存失败')
              this.loadingOptions.loading = false
              this.loadingOptions.loadingText = '保存'
              this.loadingOptions.isDisabled = false
            }
          }).catch(() => {
            this.loadingOptions.loading = false
            this.loadingOptions.loadingText = '保存'
            this.loadingOptions.isDisabled = false
          })
        }
      })
    },
    // 更新数据
    updateData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.form)
          user.resourceUpdate(tempData).then((response) => {
            if (response.success) {
              this.$message.success('保存成功')
              setTimeout(() => {
                // 2秒后跳转列表页
                this.$emit('showCard', this.showOptions)
              }, 2000)
            } else {
              this.$message.error('保存失败')
              this.loadingOptions.loading = false
              this.loadingOptions.loadingText = '保存'
              this.loadingOptions.isDisabled = false
            }
          }).catch(() => {
            this.loadingOptions.loading = false
            this.loadingOptions.loadingText = '保存'
            this.loadingOptions.isDisabled = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card ::v-deep .el-card__body {
  height: calc(100vh - 230px);
  overflow-y: auto;
}
</style>
